import axios from 'axios'
import qs from 'qs'
// import {useStore} from 'vuex'
// const url = process.env.VUE_APP_BASE_API
//   axios.defaults.baseURL= '/api'
// //   'localhost:8080/'
//   axios.defaults.timeout= 5000

const url = import.meta.env.VITE_IS_DEV ? import.meta.env.VITE_APP_URL : import.meta.env.VITE_PRO_URL ;
const service = axios.create({
    baseURL: '/api',
    timeout: 5000
})
// const store = useStore()
// console.log(store);

service.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

//请求拦截器 
service.interceptors.request.use(
    config => {
      const token = sessionStorage.getItem('token')
      token && (config.headers.Authorization = token)
      return config
    },
    error => {
        console.log(111);
      return Promise.reject(error)
    }
  )

//响应拦截器 
service.interceptors.response.use(
    response => {
      const res = response.data
      if (res.status !== 0) {
        return Promise.reject(new Error(res.message || 'Error'))
      } else {
        return res.message ? res.message : res
      }
    },
    error => {
      return Promise.reject(error)
    }
  )
  
  export function get(url: string,params: any) {
      return new Promise((resolve,reject) => {
        service.get(url,{
              params:params
          }).then(res => {
              resolve(res)
          }).catch(err => {
              reject(err)
          })
      })
  } 
  export function post(url: string,params?: any) {
      return new Promise((resolve,reject)=> {
        service.post(url,qs.stringify(params)).then(res=> {
              resolve(res)
          }).catch(err => {
              reject(err)
          } )
      })
  }

